package com.titaniume.rabbitmq.springbootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @ProjectName: rabbitmq
 * @Package: com.titaniume.rabbitmq.springbootrabbitmq.config
 * @ClassName: TtlQueueConfig
 * @Author: titan
 * @Description: 队列配置
 * @Date: 2021/12/24 10:05
 * @Version: 1.0
 */
@Configuration
public class TtlQueueConfig {

    //普通交换机的名称
    private static final String X_EXCHANGE = "X";

    //死信交换机名称
    private static final String Y_DEAD_LETTER_EXCHANGE = "Y";

    //普通队列名称
    private static final String QUEUE_A = "QA";
    private static final String QUEUE_B = "QB";

    //死信队列名称
    private static final String DEAD_LETTER_QUEUE = "QD";


    //声明xExchange
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return  new DirectExchange(X_EXCHANGE);
    }

    //声明yExchange
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return  new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }


    //声明普通队列A TTL为10s
    @Bean("queueA")
    public Queue queueA(){
        HashMap<String, Object> arguments = new HashMap<>();
        //声明当前队列绑定的死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //声明当前队列的死信路由key
        arguments.put("x-dead-letter-routing-key","YD");
        //声明队列的TTL
        arguments.put("x-message-ttl",10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    //声明队列A绑定X交换机
    @Bean
    public Binding queueaABVindingX(@Qualifier("queueA") Queue queueA,
                                 @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }



    //声明普通队列B ttl 为40m并绑定到对于的死信交换机
    @Bean("queueB")
    public Queue queueB(){
        HashMap<String, Object> arguments = new HashMap<>();
        //声明档期队列绑定的死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //声明当前队列的死信路由key
        arguments.put("x-dead-letter-routing-key","YD");
        //声明队列的TTL
        arguments.put("x-message-ttl",40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }


    //声明队列B绑定X交换机
    @Bean
    public Binding queuebBindingX(@Qualifier("queueB") Queue queueB,
                                 @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }


    //声明队列QD
    @Bean("queueD")
    public Queue queueD(){
      return  new Queue(DEAD_LETTER_QUEUE);
    }

    //声明死信独立QD绑定关系
    @Bean
    public Binding deadLetterBindingD(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(queueD).to(yExchange()).with("YD");
    }

}
